package main

import (
	"os"

	"github.com/rs/zerolog/log"

	"github.com/spf13/cobra"
	"techoiceness.com/aiagent/llm-gateway/internal/config"
	"techoiceness.com/aiagent/llm-gateway/internal/database"
	"techoiceness.com/aiagent/llm-gateway/internal/logger"
	"techoiceness.com/aiagent/llm-gateway/internal/server"
)

var startCmd = &cobra.Command{
	Use:   "start",
	Short: "启动 HTTP 服务器",
	Long:  `启动 LLM Gateway HTTP 服务器`,
	Run: func(cmd *cobra.Command, args []string) {
		cfg, err := config.LoadConfig()
		if err != nil {
			log.Fatal().Err(err).Msg("加载配置失败")
			os.Exit(1)
		}

		// 初始化日志系统
		logger.Initialize()

		log.Info().Msg("LLM网关启动中...")
		log.Info().Msg("配置加载成功")

		db, err := database.NewDatabase(&cfg.Database)
		if err != nil {
			log.Fatal().Err(err).Msg("数据库初始化失败")
		}

		log.Info().Msg("数据库初始化成功")

		server := server.NewServer(cfg, db)
		log.Info().Msg("服务器已创建，正在启动...")

		if err := server.Start(); err != nil {
			log.Fatal().Err(err).Msg("服务器启动失败")
		}
	},
}
